package com.tm.behavior.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.behavior.service.ApUnlikesBehaviorService;
import com.tm.behavior.service.StatisticsService;
import com.tm.media.model.dtos.BehaviorDto;
import com.tm.media.model.entity.*;
import com.tm.media.model.mapper.*;
import com.tm.media.utils.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @Author Ly
 * @Data 2023/2/25 17:06
 * @ClassName StatisticsServiceImpl
 */
@Service
public class StatisticsServiceImpl extends ServiceImpl<StatisticsMapper, StatisticsEntity> implements StatisticsService {
@Autowired
private StatisticsMapper statisticsMapper;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private ApUserFanMapper apUserFanMapper;
@Autowired
private ApUnlikesBehaviorMapper apUnlikesBehaviorMapper;
@Autowired
private ApUserMessageMapper apUserMessageMapper;
@Autowired
private ApLikesBehaviorMapper apLikesBehaviorMapper;
@Autowired
private ArticleMapper articleMapper;
@Autowired
private ApUserMapper apUserMapper;

    /**
     * 点赞
     * @param behaviorDto
     * @return
     */
    @Override
    public Result like_behavior(BehaviorDto behaviorDto) {
        //判断当前用户有没有给这篇文章点过赞
        QueryWrapper<ApLikesBehaviorEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("behavior_entry_id",1);
        queryWrapper.eq("entry_id",behaviorDto.getEntry_id());

        ApLikesBehaviorEntity behaviorEntity = apLikesBehaviorMapper.selectOne(queryWrapper);
        //查询出来文章ID对应的图文数据表
        StatisticsEntity statistics= statisticsMapper.selectStatistics(behaviorDto);
        ApUserMessageEntity apUserMessageEntity = new ApUserMessageEntity();
        if(behaviorEntity!=null){
            return dianzan(behaviorEntity, statistics, apUserMessageEntity);
            }else {
            ApLikesBehaviorEntity apLikesBehaviorEntity = new ApLikesBehaviorEntity();
            apLikesBehaviorEntity.setOperation(3);
            return dianzan(apLikesBehaviorEntity, statistics, apUserMessageEntity);
        }
    }

    /**
     * 不喜欢
     * @param behaviorDto
     * @return
     */
    @Override
    public Result unlike_behavior(BehaviorDto behaviorDto) {


        //文章ID是4
        QueryWrapper<ApUnlikesBehaviorEntity> queryWrapper = new QueryWrapper<>();
        //假设app用户的ID是1
        queryWrapper.eq("entry_id",1);
        queryWrapper.eq("article_id",behaviorDto.getArticle_id());
        behaviorDto.setEntry_id(behaviorDto.getArticle_id());
        ApUnlikesBehaviorEntity apUnlikesBehaviorEntity = apUnlikesBehaviorMapper.selectOne(queryWrapper);
        //查询出来文章ID对应的图文数据表
        StatisticsEntity statistics= statisticsMapper.selectStatistics(behaviorDto);
        if(apUnlikesBehaviorEntity!=null){
            return buxihuan(apUnlikesBehaviorEntity, statistics);
        }else {
            ApUnlikesBehaviorEntity apUnlikesBehavior = new ApUnlikesBehaviorEntity();
            apUnlikesBehavior.setType(3);
            return buxihuan(apUnlikesBehavior, statistics);
        }
    }

    /**
     * 关注
     * @param behaviorDto
     * @return
     */
    @Override
    public Result user_follow(BehaviorDto behaviorDto) {
        //文章ID是4
        QueryWrapper<ApUserFanEntity> queryWrapper = new QueryWrapper<>();
        //假设app用户的ID是1
        queryWrapper.eq("fans_id",3);
        queryWrapper.eq("user_id",behaviorDto.getAuthor_id());
        behaviorDto.setEntry_id(behaviorDto.getArticle_id());
        ApUserFanEntity apUserFanEntity = apUserFanMapper.selectOne(queryWrapper);
        //查询出来图文数据表
        behaviorDto.setEntry_id(behaviorDto.getArticle_id());
        //查询出来文章ID对应的图文数据表
        StatisticsEntity statistics= statisticsMapper.selectStatistics(behaviorDto);

           return   guanzhu(apUserFanEntity,statistics);
    }
    public Result guanzhu(ApUserFanEntity apUserFanEntity, StatisticsEntity statistics){
            if (apUserFanEntity!=null){
                statistics.setFollow(statistics.getFollow()-1);
                redisTemplate.opsForValue().decrement("关注"+statistics.getArticle(),-1);
                //删除关注的信息
                //看一下当前用户的姓名
                apUserFanMapper.deleteById(apUserFanEntity.getId());
            }else {
                statistics.setFollow(statistics.getFollow()+1);
                redisTemplate.opsForValue().set("关注"+statistics.getArticle(),1);
                //新增关注表
                apUserFanEntity.setUserId(statistics.getUserId());
                apUserFanEntity.setFansId(1);
                apUserFanEntity.setLevel(0);
                //看一下当前用户的姓名
                QueryWrapper<ApUserEntity> apUserQueryWarpe = new QueryWrapper<>();
                apUserQueryWarpe.eq("id",1);
                ApUserEntity apUserEntity = apUserMapper.selectOne(apUserQueryWarpe);
                apUserFanEntity.setFansName(apUserEntity.getName());
                apUserFanMapper.insert(apUserFanEntity);
            }
            statisticsMapper.updateById(statistics);
            return Result.SUCCESS(200,"成功");
    }

    /**
     * 点赞方法
     * @param behaviorEntity
     * @param statistics
     * @param apUserMessageEntity
     * @return
     */
    public Result dianzan(ApLikesBehaviorEntity behaviorEntity,StatisticsEntity statistics,ApUserMessageEntity apUserMessageEntity){
        if (behaviorEntity.getOperation().equals(1)){//当前状态为取消点赞
            if (redisTemplate.hasKey("点赞"+statistics.getArticle())){//点过赞了。加1
                redisTemplate.opsForValue().increment("点赞"+statistics.getArticle(),1);
                statistics.setLikes(statistics.getLikes()+1);
                behaviorEntity.setBehaviorEntryId(1);
                behaviorEntity.setEntryId(statistics.getArticle());
                behaviorEntity.setOperation(0);
                apLikesBehaviorMapper.updateById(behaviorEntity);
            }
            apUserMessageEntity.setContent("xxx在"+ LocalDateTime.now()+"对"+statistics.getAuthorName()+"进行点赞文章");
            apUserMessageEntity.setSenderId(statistics.getUserId());
            apUserMessageEntity.setIsRead(0);
            apUserMessageEntity.setType(2);
            apUserMessageEntity.setUserId(1);
            apUserMessageEntity.setSenderName(statistics.getAuthorName());
        }else if (behaviorEntity.getOperation().equals(0)){//点过赞了
            if (redisTemplate.hasKey("点赞"+statistics.getArticle())){
                redisTemplate.opsForValue().increment("点赞"+statistics.getArticle(),-1);
                statistics.setLikes(statistics.getLikes()-1);
                behaviorEntity.setBehaviorEntryId(1);
                behaviorEntity.setEntryId(statistics.getArticle());
                behaviorEntity.setOperation(1);
                apLikesBehaviorMapper.updateById(behaviorEntity);
            }
            apUserMessageEntity.setContent("xxx在"+ LocalDateTime.now()+"对您进行取消点赞文章");
            apUserMessageEntity.setSenderId(statistics.getUserId());
            apUserMessageEntity.setIsRead(0);
            apUserMessageEntity.setType(3);
            apUserMessageEntity.setUserId(1);
            apUserMessageEntity.setSenderName(statistics.getAuthorName());
        }else {
                redisTemplate.opsForValue().set("点赞"+statistics.getArticle(),1);
                //新增点赞
            behaviorEntity.setBehaviorEntryId(1);
            statistics.setLikes(statistics.getLikes()+1);
            behaviorEntity.setEntryId(statistics.getArticle());
            behaviorEntity.setOperation(0);
            apLikesBehaviorMapper.insert(behaviorEntity);
            //操作信息
                apUserMessageEntity.setContent("xxx在"+ LocalDateTime.now()+"对"+statistics.getAuthorName()+"进行点赞文章");
                apUserMessageEntity.setSenderId(statistics.getUserId());
                apUserMessageEntity.setIsRead(0);
                apUserMessageEntity.setType(2);
                apUserMessageEntity.setUserId(1);
                apUserMessageEntity.setSenderName(statistics.getAuthorName());
        }
        apUserMessageMapper.insert(apUserMessageEntity);
        statisticsMapper.updateById(statistics);
        return Result.SUCCESS(200,"成功");
    }

    /**
     * 不喜欢
     * @param behaviorEntity
     * @param statistics
     * @return
     */
    public Result buxihuan(ApUnlikesBehaviorEntity behaviorEntity,StatisticsEntity statistics){
            if (behaviorEntity.getType().equals(0)){//现在的状态为不喜欢
                //图文数据表里的不喜欢数量-1
                statistics.setUnlikes(statistics.getUnlikes()-1);
                //redis减一
                redisTemplate.opsForValue().increment("不喜欢"+statistics.getArticle(),-1);
                //修改行为表里的信息
                behaviorEntity.setType(1);
                behaviorEntity.setCreatedTime(LocalDateTime.now());
                apUnlikesBehaviorMapper.updateById(behaviorEntity);
            }else if (behaviorEntity.getType().equals(1)){//现在的状态为取消不喜欢
                //图文数据表里的不喜欢数量-1
                statistics.setUnlikes(statistics.getUnlikes()+1);
                //redis减一
                redisTemplate.opsForValue().increment("不喜欢"+statistics.getArticle(),1);
                //修改行为表里的信息
                behaviorEntity.setType(0);
                behaviorEntity.setCreatedTime(LocalDateTime.now());
                apUnlikesBehaviorMapper.updateById(behaviorEntity);
            }else {
                //图文统计表的不喜欢数量加一
                statistics.setUnlikes(statistics.getUnlikes()+1);
                //第一次点不喜欢存放到redis
                redisTemplate.opsForValue().set("不喜欢"+statistics.getArticle(),1);
                //存到行为表
                behaviorEntity.setType(0);
                behaviorEntity.setEntryId(1);
                behaviorEntity.setArticleId(statistics.getArticle());
                behaviorEntity.setCreatedTime(LocalDateTime.now());
                apUnlikesBehaviorMapper.insert(behaviorEntity);
            }
        statisticsMapper.updateById(statistics);
        return Result.SUCCESS(200,"成功");

    }



}
